package com.anfeng.cms.service.impl.attendance;

import com.anfeng.cms.common.Common;
import com.anfeng.cms.domain.attendance.AfRosterDetail;
import com.anfeng.cms.repository.mysql.dao.attendance.AfRosterDetailMapper;
import com.anfeng.cms.service.attendance.IAfRosterDetailService;
import com.anfeng.cms.util.IdGeneratorUtil;
import com.anfeng.cms.util.JsonMapper;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吴朋
 * @since 2019-07-18
 */
@Service
@Slf4j
public class AfRosterDetailServiceImpl extends ServiceImpl<AfRosterDetailMapper, AfRosterDetail> implements IAfRosterDetailService {

    /**
     * 新增员工排班记录
     *
     * @param rosterDetail 排班详情
     * @return
     */
    @Override
    public String addRosterInfoDetail(AfRosterDetail rosterDetail) {
        if (null == rosterDetail) {
            log.error("新增员工排班详情 失败 传入参数不能为null");
            return null;
        }
        // 判断排班人员是否存在
        AfRosterDetail detail = new AfRosterDetail();
        detail.setState(Common.DATA_STATE_NORMAL);
        detail.setRosterId(rosterDetail.getRosterId());
        detail.setEmpNo(rosterDetail.getEmpNo());

        EntityWrapper<AfRosterDetail> wrapper = new EntityWrapper<>();
        wrapper.setEntity(detail);
        AfRosterDetail afRosterDetail = this.selectOne(wrapper);
        if (afRosterDetail != null) {
            log.info("当前排班人员中已存在 该员工排班 无需再次添加 {}", JsonMapper.defaultMapper().toJson(afRosterDetail));
            return afRosterDetail.getId();
        }
        String detailId = IdGeneratorUtil.uuid();
        rosterDetail.setId(detailId);
        rosterDetail.setState(Common.DATA_STATE_NORMAL);
        Date currentDate = new Date();
        rosterDetail.setCreateTime(currentDate);
        rosterDetail.setModifiedTime(currentDate);
        boolean insert = this.insert(rosterDetail);
        if (!insert) {
            log.error("新增员工排班信息失败 保存数据库出错 {}", JsonMapper.defaultMapper().toJson(rosterDetail));
            return null;
        }
        return detailId;
    }

    /**
     * 根据排班id获取该排班的人员数量
     *
     * @param rosterId 排班id
     * @return 员工数量
     */
    @Override
    public int getRosterInfoDetailCounts(String rosterId) {
        EntityWrapper<AfRosterDetail> wrapper = this.generatorRosterDetailWrapper(rosterId);
        return this.selectCount(wrapper);
    }

    /**
     * 获取制定排班下的所有排班人员信息
     *
     * @param rosterId 排班id
     * @return 人员排班
     */
    @Override
    public List<AfRosterDetail> getRosterInfoDetails(String rosterId) {
        EntityWrapper<AfRosterDetail> wrapper = this.generatorRosterDetailWrapper(rosterId);
        return this.selectList(wrapper);
    }

    /**
     * 分页查询排班中 人员信息
     *
     * @param rosterId 排班id
     * @param page     当前页
     * @param pageSize 每页数量
     * @return 人员排班信息
     */
    @Override
    public List<AfRosterDetail> getRosterInfoDetailByPage(String rosterId, Integer page, Integer pageSize) {
        if (null == page || page < 1) {
            page = 1;
        }
        if (null == pageSize || pageSize < 10) {
            pageSize = 10;
        }
        Page<AfRosterDetail> pageInfo = new Page<>();
        pageInfo.setSize(pageSize);
        pageInfo.setCurrent(page);

        EntityWrapper<AfRosterDetail> wrapper = this.generatorRosterDetailWrapper(rosterId);
        Page<AfRosterDetail> detailPage = this.selectPage(pageInfo, wrapper);
        if (null == detailPage || null == detailPage.getRecords() || detailPage.getRecords().isEmpty()) {
            return Collections.EMPTY_LIST;
        }
        return detailPage.getRecords();
    }

    /**
     * 批量修改 长期排班信息
     *
     * @param addEmployeeNos    添加到排班中的员工编号数组
     * @param removeEmployeeNos 移除排班的员工编号数组
     * @return 是否操作成功 -1 失败 1 成功
     */
    @Override
    public int batchUpdateLongTermRosterDetailInfos(List<String> addEmployeeNos, List<String> removeEmployeeNos) {
        //TODO
        return 0;
    }

    /**
     * 批量修改临时排班人员信息
     *
     * @param addEmployeeNos    新增临时排班人员信息
     * @param removeEmployeeNos 移除临时排班人员信息
     * @return 是否操作成功 -1 失败  1 成功
     */
    @Override
    public int batchUpdateInterimRosterDetailInfos(List<String> addEmployeeNos, List<String> removeEmployeeNos) {
        //TODO
        return 0;
    }

    /**
     * 根据员工编号和班次id 获取员工排班信息
     *
     * @param empNo    员工编号
     * @param rosterId 排班id
     * @return 员工排班信息
     */
    @Override
    public AfRosterDetail getRosterInfoDetailByEmpNoAndRosterId(String empNo, String rosterId) {
        if (StringUtils.isBlank(empNo) || StringUtils.isBlank(rosterId)) {
            log.error("获取员工排班信息 参数有误 参数不能为null");
            return null;
        }
        AfRosterDetail afRosterDetail = new AfRosterDetail();
        afRosterDetail.setRosterId(rosterId);
        afRosterDetail.setEmpNo(empNo);
        afRosterDetail.setState(Common.DATA_STATE_NORMAL);
        EntityWrapper<AfRosterDetail> wrapper = new EntityWrapper<>();
        wrapper.setEntity(afRosterDetail);
        return this.selectOne(wrapper);
    }

    /**
     * 根据排班id修改排班详情中的信息
     *
     * @param rosterId 排班id
     * @param name     班次名
     * @return 是否成功 -1 失败 1 成功
     */
    @Override
    public int updateRosterInfo(String rosterId, String name) {
        if (StringUtils.isBlank(rosterId) || StringUtils.isBlank(name)) {
            log.error("批量修改排班详情信息失败 传入参数为null ");
            return Common.RETURN_FAILED;
        }
        EntityWrapper<AfRosterDetail> wrapper = this.generatorRosterDetailWrapper(rosterId);
        List<AfRosterDetail> rosterDetails = this.selectList(wrapper);
        if (null != rosterDetails && !rosterDetails.isEmpty()) {
            rosterDetails.forEach(rosterDetail -> {
                rosterDetail.setRosterName(name);
                rosterDetail.setModifiedTime(new Date());
            });
            boolean result = this.updateBatchById(rosterDetails);
            if (!result) {
                log.error("批量修改排班详情失败 rosterId: {} name:{}", rosterId, name);
                return Common.RETURN_FAILED;
            }
        }
        return Common.RETURN_SUCCESS;
    }

    /**
     * 根据排班id 生成详情查询 wrapper
     * @param rosterId 排班id
     * @return wrapper
     */
    private EntityWrapper<AfRosterDetail> generatorRosterDetailWrapper(String rosterId) {
        AfRosterDetail detail = new AfRosterDetail();
        detail.setRosterId(rosterId);
        detail.setState(Common.DATA_STATE_NORMAL);

        EntityWrapper<AfRosterDetail> wrapper = new EntityWrapper<>();
        wrapper.setEntity(detail);
        wrapper.orderBy("create_time", false);
        return wrapper;
    }
}
